package com.csi.controller;

import com.csi.annotation.Controller;
import com.csi.annotation.RequestMapping;
import com.csi.domain.UserInfo;
import com.csi.service.UserInfoService;
import com.csi.service.impl.UserInfoServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

@Controller
public class UserInfoController {

    @RequestMapping("/login.do")
    public void login(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        request.setCharacterEncoding ( "UTF-8" );
        response.setCharacterEncoding ( "UTF-8" );

        String username = request.getParameter ( "username" );
        String password = request.getParameter ( "password" );
        String code_random = request.getParameter ( "code_random" );
        String code = (String) request.getSession ().getAttribute ( "scode" );
        if ((username != null && password != null && code_random != null) && (!("".equals ( username.trim () ) && "".equals ( password.trim () ) && "".equals ( code_random.trim () )))) {
            if (code_random.equals ( code )) {
                UserInfoService userInfoService = new UserInfoServiceImpl ();
                UserInfo userInfo = userInfoService.login ( username , password );
                if (userInfo != null) {
                    HttpSession session = request.getSession ();
                    session.setAttribute ( "userInfo" , userInfo );
                    try {
                        response.sendRedirect ( "index.jsp" );
                    } catch (IOException e) {
                        e.printStackTrace ();
                    }
                } else {
                    String str = "登录失败！！！";
                    request.setAttribute ( "str" , str );
                    try {
                        request.getRequestDispatcher ( "login.jsp" ).forward ( request , response );
                    } catch (ServletException e) {
                        e.printStackTrace ();
                    } catch (IOException e) {
                        e.printStackTrace ();
                    }
                }
            }else {
                String str = "验证码错误！！！";
                request.setAttribute ( "str" , str );
                try {
                    request.getRequestDispatcher ( "login.jsp" ).forward ( request , response );
                } catch (ServletException e) {
                    e.printStackTrace ();
                } catch (IOException e) {
                    e.printStackTrace ();
                }
            }
        } else {
            String str = "登录失败！！！";
            request.setAttribute ( "str" , str );
            try {
                request.getRequestDispatcher ( "login.jsp" ).forward ( request , response );
            } catch (ServletException e) {
                e.printStackTrace ();
            } catch (IOException e) {
                e.printStackTrace ();
            }
        }
    }
}
